// @ts-nocheck
import HttpClient from "./client";
import API from "./endpoints";

function serve({
  serviceName = "",
  serviceArguments = {},
  config = {},
}): Promise<any> {
  const { method, endpoint } = API[serviceName];

  // resetful api replace params
  const url = endpoint.replace(/:([\w]+)/g, (word: string, $1: any) => {
    return serviceArguments[$1] ? serviceArguments[$1] : word;
  });

  // console.log('serviceArguments:', serviceArguments);

  return HttpClient[method.toLocaleLowerCase()]({
    url,
    payload: serviceArguments,
    config: config,
  });
}

export default Object.keys(API).reduce((pre, serviceName) => {
  pre[serviceName] = (serviceArguments: object, config: object) =>
    serve({ serviceName, serviceArguments, config });
  return pre;
}, {});
